﻿//盛派微信菜单操作
var senparc = {};
var maxSubMenuCount = 5;
var menuState;
var hiddenButtonType;
senparc.menu = {
    init: function () {
        menuState = $('#menuState');
        $('#buttonDetails').hide();
        $("#buttonDetails_type").change(senparc.menu.typeChanged);
        //为隐藏域赋值
        $(':input[id^=menu_button]').click(function () {
            $('#buttonDetails').show(100);
            //获取Id的值
            var idPrefix = $(this).attr('data-root')
                            ? ('menu_button' + $(this).attr('data-root'))
                            : ('menu_button' + $(this).attr('data-j') + '_sub_button' + $(this).attr('data-i'));

            var keyId = idPrefix + "_key";//key
            var nameId = idPrefix + "_name";//菜单名称
            var typeId = idPrefix + "_type";//菜单类别id
            var urlId = idPrefix + "_url";//网址
            var appidId = idPrefix + "_appid";//小程序appid
            var pagepathId = idPrefix + "_pagepath";//小程序路径

            var txtDetailsKey = $('#buttonDetails_key');//key
            var txtDetailsName = $('#buttonDetails_name');//菜单名称
            var ddlDetailsType = $('#buttonDetails_type');//菜单类别id
            var txtDetailsUrl = $('#buttonDetails_url');//网址
            var txtDetailsAppid = $('#buttonDetails_appid');//小程序appid
            var txtDetailsPagepath = $('#buttonDetails_pagepath');//小程序路径

            var hiddenButtonKey = $('#' + keyId);//key
            hiddenButtonType = $('#' + typeId);//菜单类别id
            var hiddenButtonUrl = $('#' + urlId);//网址
            var hiddenButtonAppid = $('#' + appidId);//小程序appid
            var hiddenButtonPagepath = $('#' + pagepathId);//小程序路径

            txtDetailsKey.val(hiddenButtonKey.val());//key
            txtDetailsName.val($('#' + nameId).val());//菜单名称
            ddlDetailsType.val(hiddenButtonType.val());//菜单类别id
            txtDetailsUrl.val(hiddenButtonUrl.val());//网址
            txtDetailsAppid.val(hiddenButtonAppid.val());//小程序appid
            txtDetailsPagepath.val(hiddenButtonPagepath.val());//小程序路径

            senparc.menu.typeChanged();
            txtDetailsKey.unbind('blur').blur(function () {
                hiddenButtonKey.val($(this).val());
            });
            ddlDetailsType.unbind('blur').blur(function () {
                hiddenButtonType.val($(this).val());
            });
            txtDetailsUrl.unbind('blur').blur(function () {
                hiddenButtonUrl.val($(this).val());
            });
            txtDetailsAppid.unbind('blur').blur(function () {
                hiddenButtonAppid.val($(this).val());
            });
            txtDetailsPagepath.unbind('blur').blur(function () {
                hiddenButtonPagepath.val($(this).val());
            });
        });

        //获取菜单
        $('#btnGetMenu').click(function () {
            menuState.html('获取菜单中...');
            $.getJSON('/Admin/MenuOperator/GetMenu?t=' + Math.random(), {
                group_id: $('#group_id').val(),
                sex: $('#sex').val(),
                country: $('#country').val(),
                province: $('#province').val(),
                city: $('#city').val(),
                client_platform_type: $('#client_platform_type').val()
            }, function (json) {
                //显示JSON
                $('#txtReveiceJSON').html(JSON.stringify(json));
                if (json.menu) {
                    $(':input[id^=menu_button]:not([id$=_type])').val('');
                    $('#buttonDetails:input').val('');
                    var buttons = json.menu.button;
                    //此处i与j和页面中反转
                    for (var i = 0; i < buttons.length; i++) {
                        var button = buttons[i];
                        $('#menu_button' + i + '_name').val(button.name);
                        $('#menu_button' + i + '_key').val(button.key);
                        $('#menu_button' + i + '_type').val(button.type || 'click');
                        $('#menu_button' + i + '_url').val(button.url);
                        if (button.sub_button && button.sub_button.length > 0) {
                            //二级菜单
                            for (var j = 0; j < button.sub_button.length; j++) {
                                var subButton = button.sub_button[j];
                                var idPrefix = '#menu_button' + i + '_sub_button' + (5 - button.sub_button.length + j);
                                $(idPrefix + "_name").val(subButton.name);
                                $(idPrefix + "_type").val(subButton.type || 'click');
                                $(idPrefix + "_key").val(subButton.key);
                                $(idPrefix + "_url").val(subButton.url);
                                $(idPrefix + "_appid").val(subButton.appid);
                                $(idPrefix + "_pagepath").val(subButton.pagepath);
                            }
                        } else {
                            //底部菜单
                            //...
                        }
                    }
                    menuState.html('默认菜单获取已完成');
                } else {
                    $(':input[id^=menu_button]:not([id$=_type])').val('');
                    $('#buttonDetails:input').val('');
                    var buttons = json.button;
                    //此处i与j和页面中反转
                    for (var i = 0; i < buttons.length; i++) {
                        var button = buttons[i];
                        $('#menu_button' + i + '_name').val(button.name);
                        $('#menu_button' + i + '_key').val(button.key);
                        $('#menu_button' + i + '_type').val(button.type || 'click');
                        $('#menu_button' + i + '_url').val(button.url);
                        if (button.sub_button && button.sub_button.length > 0) {                            
                            //二级菜单
                            for (var j = 0; j < button.sub_button.length; j++) {
                                var subButton = button.sub_button[j];
                                var idPrefix = '#menu_button' + i + '_sub_button' + (5 - button.sub_button.length + j);
                                $(idPrefix + "_name").val(subButton.name);
                                $(idPrefix + "_type").val(subButton.type || 'click');
                                $(idPrefix + "_key").val(subButton.key);
                                $(idPrefix + "_url").val(subButton.url);
                                $(idPrefix + "_appid").val(subButton.appid);
                                $(idPrefix + "_pagepath").val(subButton.pagepath);
                            }
                        }
                    }
                    menuState.html('个性化菜单获取完成');
                }
            }).success(function () { }).error(function () {
                menuState.html('获取数据出错,本组中可能无菜单');
            });
        });

        $('#btnDeleteMenu').click(function () {
            if (!confirm('确定要删除菜单吗？此操作无法撤销！')) {
                return false;
            }

            menuState.html('删除菜单中...');
            $.getJSON('/Admin/MenuOperator/DeleteMenu?t=' + Math.random(), {}, function (json) {
                if (json.Success) {
                    menuState.html('删除成功，如果是误删，并且界面上有最新的菜单状态，可以立即点击【更新到服务器】按钮。');
                } else {
                    menuState.html(json.Message);
                }
            });
        });

        $('#submitMenu').click(function () {
            if (!confirm('确定要提交吗？此操作无法撤销！')) {
                return false;
            }

            menuState.html('上传中...');

            $('#form_Menu').ajaxSubmit({
                dataType: 'json',
                success: function (json) {
                    if (json.Successed) {
                        menuState.html('上传成功');
                    } else {
                        menuState.html(json.Message);
                    }
                }
            });
        });
    },
    typeChanged: function () {
        var val = $('#buttonDetails_type').val().toUpperCase();
        hiddenButtonType.val($('#buttonDetails_type').val());
        console.log(val);
        switch (val) {
            case 'CLICK':
                $('#buttonDetails_key_area').slideDown(100);
                $('#buttonDetails_url_area').slideUp(100);
                $('#buttonDetails_miniprogram_appid_area').slideUp(100);
                $('#buttonDetails_miniprogram_pagepath_area').slideUp(100);
                break;
            case 'VIEW':
                $('#buttonDetails_key_area').slideUp(100);
                $('#buttonDetails_url_area').slideDown(100);
                $('#buttonDetails_miniprogram_appid_area').slideUp(100);
                $('#buttonDetails_miniprogram_pagepath_area').slideUp(100);
                break;
            case 'MINIPROGRAM':
                $('#buttonDetails_key_area').slideUp(100);
                $('#buttonDetails_url_area').slideDown(100);
                $('#buttonDetails_miniprogram_appid_area').slideDown(100);
                $('#buttonDetails_miniprogram_pagepath_area').slideDown(100);
                break;
            case 'MEDIA_ID':
            case 'VIEW_LIMITED':
                $('#buttonDetails_key_area').slideUp(100);
                $('#buttonDetails_url_area').slideUp(100);
                $('#buttonDetails_miniprogram_appid_area').slideUp(100);
                $('#buttonDetails_miniprogram_pagepath_area').slideUp(100);
                break;
            default:
                $('#buttonDetails_key_area').slideDown(100);
                $('#buttonDetails_url_area').slideUp(100);
                $('#buttonDetails_miniprogram_appid_area').slideUp(100);
                $('#buttonDetails_miniprogram_pagepath_area').slideUp(100);
                break;
        }
    }
};